data <- read.csv("trolley_replication_data.csv")

############################################################
#Figure 1
#first is 1 to be sacrificed, second is identify of 5
############################################################

#first is 1 to be sacrificed, seconnd is identify of 5
prop.copartisan.copartisan <- mean(data$flip[data$copartisan1 == 1 & data$copartisan5 == 1], na.rm = T)
prop.noncopartisan.copartisan <- mean(data$flip[data$outpartisan1 == 1 & data$copartisan5 == 1], na.rm = T)

prop.copartisan.noncopartisan <- mean(data$flip[data$copartisan1 == 1 & data$outpartisan5 == 1], na.rm = T)
prop.noncopartisan.noncopartisan <- mean(data$flip[data$outpartisan1 == 1 & data$outpartisan5 == 1], na.rm = T)

prop.copartisan.copartisan
prop.noncopartisan.copartisan
prop.copartisan.noncopartisan
prop.noncopartisan.noncopartisan

par(mar = c(5.1, 4.1, 4.1, 2.1))
plot(x=c(1,2,3,4), y = c(0, 1, 0, 1), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "Sacrificial Individual", xlim = c(.75, 4.25), ylim = c(0., 1.1))
axis(1, at = c(1, 2, 3, 4), labels = c("1-copartisan", "1-outpartisan", "1-copartisan", "1-outpartisan"))
axis(2, at = seq(0, 1, .2), las = 2)
text(1.5, 1.1, "To save 5-copartisans")
text(3.5, 1.1, "To save 5-outpartisans")
segments(x0=1, x1=1, y0=0, y1=prop.copartisan.copartisan, lwd = 25, lend = "butt")
segments(x0=2, x1=2, y0=0, y1=prop.noncopartisan.copartisan, lwd = 25, lend = "butt")
segments(x0=3, x1=3, y0=0, y1=prop.copartisan.noncopartisan, lwd = 25, lend = "butt")
segments(x0=4, x1=4, y0=0, y1=prop.noncopartisan.noncopartisan, lwd = 25, lend = "butt")
segments(x0=1, x1=1, y0=.8688621, y1=.9311379, lwd = 3, lend = "butt", col = "grey")
segments(x0=2, x1=2, y0=.8970956, y1=.9520664, lwd = 3, lend = "butt", col = "grey")
segments(x0=3, x1=3, y0=.5385247, y1=.6446585, lwd = 3, lend = "butt", col = "grey")
segments(x0=4, x1=4, y0=.7017327, y1=.7952733, lwd = 3, lend = "butt", col = "grey")
abline(v = 2.5, lty = 2, col = "light grey")
text(4, prop.noncopartisan.noncopartisan + .08, round(prop.noncopartisan.noncopartisan, 2))
text(3, prop.copartisan.noncopartisan + .08, round(prop.copartisan.noncopartisan, 2))
text(2, prop.noncopartisan.copartisan + .08, round(prop.noncopartisan.copartisan, 2))
text(1, prop.copartisan.copartisan + .08, round(prop.copartisan.copartisan, 2))
box()


############################################################
#Figure 1 split by party of respondent
#first is 1 to be sacrificed, second is identify of 5
############################################################

prop.copartisan.copartisan.r <- mean(data$flip[data$copartisan1 == 1 & data$copartisan5 == 1 & data$republican == 1], na.rm = T)
prop.noncopartisan.copartisan.r <- mean(data$flip[data$outpartisan1 == 1 & data$copartisan5 == 1 & data$republican == 1], na.rm = T)
prop.copartisan.noncopartisan.r <- mean(data$flip[data$copartisan1 == 1 & data$outpartisan5 == 1 & data$republican == 1], na.rm = T)
prop.noncopartisan.noncopartisan.r <- mean(data$flip[data$outpartisan1 == 1 & data$outpartisan5 == 1 & data$republican == 1], na.rm = T)

prop.copartisan.copartisan.d <- mean(data$flip[data$copartisan1 == 1 & data$copartisan5 == 1 & data$democrat == 1], na.rm = T)
prop.noncopartisan.copartisan.d <- mean(data$flip[data$outpartisan1 == 1 & data$copartisan5 == 1 & data$democrat == 1], na.rm = T)
prop.copartisan.noncopartisan.d <- mean(data$flip[data$copartisan1 == 1 & data$outpartisan5 == 1 & data$democrat == 1], na.rm = T)
prop.noncopartisan.noncopartisan.d <- mean(data$flip[data$outpartisan1 == 1 & data$outpartisan5 == 1 & data$democrat == 1], na.rm = T)

summary(model1 <- lm(flip ~ 1, data = data[data$copartisan1 == 1 & data$copartisan5 == 1 & data$republican == 1,]))
summary(model2 <- lm(flip ~ 1, data = data[data$outpartisan1 == 1 & data$copartisan5 == 1 & data$republican == 1,]))
summary(model3 <- lm(flip ~ 1, data = data[data$copartisan1 == 1 & data$outpartisan5 == 1 & data$republican == 1,]))
summary(model4 <- lm(flip ~ 1, data = data[data$outpartisan1 == 1 & data$outpartisan5 == 1 & data$republican == 1,]))
summary(model5 <- lm(flip ~ 1, data = data[data$copartisan1 == 1 & data$copartisan5 == 1 & data$democrat == 1,]))
summary(model6 <- lm(flip ~ 1, data = data[data$outpartisan1 == 1 & data$copartisan5 == 1 & data$democrat == 1,]))
summary(model7 <- lm(flip ~ 1, data = data[data$copartisan1 == 1 & data$outpartisan5 == 1 & data$democrat == 1,]))
summary(model8 <- lm(flip ~ 1, data = data[data$outpartisan1 == 1 & data$outpartisan5 == 1 & data$democrat == 1,]))
se1 <- sqrt(vcov(model1))
se2 <- sqrt(vcov(model2))
se3 <- sqrt(vcov(model3))
se4 <- sqrt(vcov(model4))
se5 <- sqrt(vcov(model5))
se6 <- sqrt(vcov(model6))
se7 <- sqrt(vcov(model7))
se8 <- sqrt(vcov(model8))

conf1 <- c(prop.copartisan.copartisan.r - 1.96*se1, prop.copartisan.copartisan.r + 1.96*se1)
conf2 <- c(prop.copartisan.copartisan.d - 1.96*se5, prop.copartisan.copartisan.d + 1.96*se5)
conf3 <- c(prop.noncopartisan.copartisan.r - 1.96*se2, prop.noncopartisan.copartisan.r + 1.96*se2)
conf4 <- c(prop.noncopartisan.copartisan.d - 1.96*se6, prop.noncopartisan.copartisan.d + 1.96*se6)
conf5 <- c(prop.copartisan.noncopartisan.r - 1.96*se3, prop.copartisan.noncopartisan.r + 1.96*se3)
conf6 <- c(prop.copartisan.noncopartisan.d - 1.96*se7, prop.copartisan.noncopartisan.d + 1.96*se7)
conf7 <- c(prop.noncopartisan.noncopartisan.r - 1.96*se4, prop.noncopartisan.noncopartisan.r + 1.96*se4)
conf8 <- c(prop.noncopartisan.noncopartisan.d - 1.96*se8, prop.noncopartisan.noncopartisan.d + 1.96*se8)

#begin the plot
plot(x=c(1,2,3,4), y = c(0, 1, 0, 1), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 To Save 5", xlab = "Sacrificial Individual", xlim = c(.75, 4.25), ylim = c(0, 1.1))
axis(1, at = c(1, 2, 3, 4), labels = c("1-copartisan", "1-outpartisan", "1-copartisan", "1-outpartisan"))
axis(2, at = seq(0, 1, .2), las = 2)
text(1.5, 1.1, "To save 5-copartisans")
text(3.5, 1.1, "To save 5-outpartisans")
segments(x0=.9, x1=.9, y0=0, y1=prop.copartisan.copartisan.r, lwd = 23, lend = "butt")
segments(x0=1.1, x1=1.1, y0=0, y1=prop.copartisan.copartisan.d, lwd = 23, lend = "butt", col = "grey")
segments(x0=1.9, x1=1.9, y0=0, y1=prop.noncopartisan.copartisan.r, lwd = 23, lend = "butt")
segments(x0=2.1, x1=2.1, y0=0, y1=prop.noncopartisan.copartisan.d, lwd = 23, lend = "butt", col = "grey")
segments(x0=2.9, x1=2.9, y0=0, y1=prop.copartisan.noncopartisan.r, lwd = 23, lend = "butt")
segments(x0=3.1, x1=3.1, y0=0, y1=prop.copartisan.noncopartisan.d, lwd = 23, lend = "butt", col = "grey")
segments(x0=3.9, x1=3.9, y0=0, y1=prop.noncopartisan.noncopartisan.r, lwd = 23, lend = "butt")
segments(x0=4.1, x1=4.1, y0=0, y1=prop.noncopartisan.noncopartisan.d, lwd = 23, lend = "butt", col = "grey")

segments(x0=.9, x1=.9, y0=conf1[1], y1=conf1[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=1.1, x1=1.1, y0=conf2[1], y1=conf2[2], lwd = 3, lend = "butt", col = "black")
segments(x0=1.9, x1=1.9, y0=conf3[1], y1=conf3[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=2.1, x1=2.1, y0=conf4[1], y1=conf4[2], lwd = 3, lend = "butt", col = "black")
segments(x0=2.9, x1=2.9, y0=conf5[1], y1=conf5[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=3.1, x1=3.1, y0=conf6[1], y1=conf6[2], lwd = 3, lend = "butt", col = "black")
segments(x0=3.9, x1=3.9, y0=conf7[1], y1=conf7[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=4.1, x1=4.1, y0=conf8[1], y1=conf8[2], lwd = 3, lend = "butt", col = "black")

text(x=.9, y=prop.copartisan.copartisan.r + .1, round(prop.copartisan.copartisan.r, 2), cex = .7)
text(x=1.1, y=prop.copartisan.copartisan.d + .1, round(prop.copartisan.copartisan.d, 2), cex = .7)
text(x=1.9, y=prop.noncopartisan.copartisan.r + .1, round(prop.noncopartisan.copartisan.r, 2), cex = .7)
text(x=2.1, y=prop.noncopartisan.copartisan.d + .1, round(prop.noncopartisan.copartisan.d, 2), cex = .7)
text(x=2.9, y=prop.copartisan.noncopartisan.r + .13, round(prop.copartisan.noncopartisan.r, 2), cex = .7)
text(x=3.1, y=prop.copartisan.noncopartisan.d + .11, round(prop.copartisan.noncopartisan.d, 2), cex = .7)
text(x=3.9, y=prop.noncopartisan.noncopartisan.r + .11, round(prop.noncopartisan.noncopartisan.r, 2), cex = .7)
text(x=4.1, y=prop.noncopartisan.noncopartisan.d + .11, round(prop.noncopartisan.noncopartisan.d, 2), cex = .7)

abline(v = 2.5, lty = 2, col = "light grey")
text(.9, .28, srt = 90, "Republican Respondents", col = "white")
text(1.1, .28, srt = 90, "Democratic Respondents", col = "black")

box()


###################################################
#Figure 2
#add all other identities
#to save 5 copartisans
###################################################
prop.crime.copartisan <- mean(data$flip[data$onecrim == 1 & data$copartisan5 == 1], na.rm = T)
prop.immigrant.copartisan <- mean(data$flip[data$oneimm == 1 & data$copartisan5 == 1], na.rm = T)
prop.nazi.copartisan <- mean(data$flip[data$onenazi == 1 & data$copartisan5 == 1], na.rm = T)
prop.dog.copartisan <- mean(data$flip[data$onedog == 1 & data$copartisan5 == 1], na.rm = T)
prop.noncitizen.copartisan <- mean(data$flip[data$onenon == 1 & data$copartisan5 == 1], na.rm = T)

summary(model <- lm(flip ~ onenazi + onecrim + copartisan1 + onenon + oneimm + onedog, data = data[data$copartisan5 == 1,]))

summary(model1 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$outpartisan1 == 1,]))
summary(model2 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$onenazi == 1,]))
summary(model3 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$onecrim == 1,]))
summary(model4 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$copartisan1 == 1,]))
summary(model5 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$onenon == 1,]))
summary(model6 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$oneimm == 1,]))
summary(model7 <- lm(flip ~ 1, data = data[data$copartisan5 == 1 & data$onedog == 1,]))

se1 <- sqrt(vcov(model1))
se2 <- sqrt(vcov(model2))
se3 <- sqrt(vcov(model3))
se4 <- sqrt(vcov(model4))
se5 <- sqrt(vcov(model5))
se6 <- sqrt(vcov(model6))
se7 <- sqrt(vcov(model7))

conf1 <- c(prop.noncopartisan.copartisan - 1.96*se1, prop.noncopartisan.copartisan + 1.96*se1)
conf2 <- c(prop.nazi.copartisan - 1.96*se2, prop.nazi.copartisan + 1.96*se2)
conf3 <- c(prop.crime.copartisan - 1.96*se3, prop.crime.copartisan + 1.96*se3)
conf4 <- c(prop.copartisan.copartisan - 1.96*se4, prop.copartisan.copartisan + 1.96*se4)
conf5 <- c(prop.noncitizen.copartisan - 1.96*se5, prop.noncitizen.copartisan + 1.96*se5)
conf6 <- c(prop.immigrant.copartisan - 1.96*se6, prop.immigrant.copartisan + 1.96*se6)
conf7 <- c(prop.dog.copartisan - 1.96*se7, prop.dog.copartisan + 1.96*se7)


par(mar = c(8, 4, 4, 2))
plot(x=c(1,2,3,4,5,6,7), y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Co-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "copartisan", "non-citizen",  "illegal\nimmigrant", "golden\nretreiver"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)
segments(x0=1, x1=1, y0=0, y1=prop.noncopartisan.copartisan, lwd = 25, lend = "butt")
segments(x0=2, x1=2, y0=0, y1=prop.nazi.copartisan, lwd = 25, lend = "butt")
segments(x0=3, x1=3, y0=0, y1=prop.crime.copartisan, lwd = 25, lend = "butt")
segments(x0=4, x1=4, y0=0, y1=prop.copartisan.copartisan, lwd = 25, lend = "butt")
segments(x0=5, x1=5, y0=0, y1=prop.noncitizen.copartisan, lwd = 25, lend = "butt")
segments(x0=6, x1=6, y0=0, y1=prop.immigrant.copartisan, lwd = 25, lend = "butt")
segments(x0=7, x1=7, y0=0, y1=prop.dog.copartisan, lwd = 25, lend = "butt")

segments(x0=1, x1=1, y0=conf1[1], y1=conf1[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=2, x1=2, y0=conf2[1], y1=conf2[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=3, x1=3, y0=conf3[1], y1=conf3[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=4, x1=4, y0=conf4[1], y1=conf4[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=5, x1=5, y0=conf5[1], y1=conf5[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=6, x1=6, y0=conf6[1], y1=conf6[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=7, x1=7, y0=conf7[1], y1=conf7[2], lwd = 3, lend = "butt", col = "grey")

abline(h = prop.noncopartisan.copartisan, lty = 2, col = "grey")

points(4, prop.copartisan.copartisan +.06, pch = 8)
points(5, prop.noncitizen.copartisan +.08, pch = 8)
points(6, prop.immigrant.copartisan +.08, pch = 8)
points(7, prop.dog.copartisan +.08, pch = 8)
mtext(side = 1, at = 4, line = 5, "Sacrificial Individual")
box()

text(1, 1, round(prop.noncopartisan.copartisan, 2), cex = .7)
text(2, 1, round(prop.nazi.copartisan, 2), cex = .7)
text(3, 1, round(prop.crime.copartisan, 2), cex = .7)
text(4, 1, round(prop.copartisan.copartisan, 2), cex = .7)
text(5, 1, round(prop.noncitizen.copartisan, 2), cex = .7)
text(6, 1, round(prop.immigrant.copartisan, 2), cex = .7)
text(7, 1, round(prop.dog.copartisan, 2), cex = .7)


###################################################
#Figure 2b
#now for saving 5 outpartisans
###################################################
prop.crime.noncopartisan <- mean(data$flip[data$onecrim == 1 & data$outpartisan5 == 1], na.rm = T)
prop.immigrant.noncopartisan <- mean(data$flip[data$oneimm == 1 & data$outpartisan5 == 1], na.rm = T)
prop.nazi.noncopartisan <- mean(data$flip[data$onenazi == 1 & data$outpartisan5 == 1], na.rm = T)
prop.dog.noncopartisan <- mean(data$flip[data$onedog == 1 & data$outpartisan5 == 1], na.rm = T)
prop.noncitizen.noncopartisan <- mean(data$flip[data$onenon == 1 & data$outpartisan5 == 1], na.rm = T)

summary(model <- lm(flip ~ onenazi + onecrim + copartisan1 + onenon + oneimm + onedog, data = data[data$outpartisan5 == 1,]))

summary(model1 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$outpartisan1 == 1,]))
summary(model2 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$onenazi == 1,]))
summary(model3 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$onecrim == 1,]))
summary(model4 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$copartisan1 == 1,]))
summary(model5 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$onenon == 1,]))
summary(model6 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$oneimm == 1,]))
summary(model7 <- lm(flip ~ 1, data = data[data$outpartisan5 == 1 & data$onedog == 1,]))

se1 <- sqrt(vcov(model1))
se2 <- sqrt(vcov(model2))
se3 <- sqrt(vcov(model3))
se4 <- sqrt(vcov(model4))
se5 <- sqrt(vcov(model5))
se6 <- sqrt(vcov(model6))
se7 <- sqrt(vcov(model7))

conf1o <- c(prop.noncopartisan.noncopartisan - 1.96*se1, prop.noncopartisan.noncopartisan + 1.96*se1)
conf2o <- c(prop.nazi.noncopartisan - 1.96*se2, prop.nazi.noncopartisan + 1.96*se2)
conf3o <- c(prop.crime.noncopartisan - 1.96*se3, prop.crime.noncopartisan + 1.96*se3)
conf4o <- c(prop.copartisan.noncopartisan - 1.96*se4, prop.copartisan.noncopartisan + 1.96*se4)
conf5o <- c(prop.noncitizen.noncopartisan - 1.96*se5, prop.noncitizen.noncopartisan + 1.96*se5)
conf6o <- c(prop.immigrant.noncopartisan - 1.96*se6, prop.immigrant.noncopartisan + 1.96*se6)
conf7o <- c(prop.dog.noncopartisan - 1.96*se7, prop.dog.noncopartisan + 1.96*se7)

par(mar = c(8, 4, 4, 2))
plot(x=c(1,2,3,4,5,6,7), y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Out-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "copartisan", "non-citizen",  "illegal\nimmigrant", "golden\nretreiver"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)
segments(x0=1, x1=1, y0=0, y1=prop.noncopartisan.noncopartisan, lwd = 25, lend = "butt")
segments(x0=2, x1=2, y0=0, y1=prop.nazi.noncopartisan, lwd = 25, lend = "butt")
segments(x0=3, x1=3, y0=0, y1=prop.crime.noncopartisan, lwd = 25, lend = "butt")
segments(x0=4, x1=4, y0=0, y1=prop.copartisan.noncopartisan, lwd = 25, lend = "butt")
segments(x0=5, x1=5, y0=0, y1=prop.noncitizen.noncopartisan, lwd = 25, lend = "butt")
segments(x0=6, x1=6, y0=0, y1=prop.immigrant.noncopartisan, lwd = 25, lend = "butt")
segments(x0=7, x1=7, y0=0, y1=prop.dog.noncopartisan, lwd = 25, lend = "butt")

segments(x0=1, x1=1, y0=conf1o[1], y1=conf1o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=2, x1=2, y0=conf2o[1], y1=conf2o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=3, x1=3, y0=conf3o[1], y1=conf3o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=4, x1=4, y0=conf4o[1], y1=conf4o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=5, x1=5, y0=conf5o[1], y1=conf5o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=6, x1=6, y0=conf6o[1], y1=conf6o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=7, x1=7, y0=conf7o[1], y1=conf7o[2], lwd = 3, lend = "butt", col = "grey")

abline(h = prop.noncopartisan.noncopartisan, lty = 2, col = "grey")

points(2, prop.nazi.noncopartisan +.08, pch = 8)
points(4, prop.copartisan.noncopartisan + .08, pch = 8)
points(5, prop.noncitizen.noncopartisan + .08, pch = 8)
mtext(side = 1, at = 4, line = 5, "Sacrificial Individual")
box()

text(1, 1, round(prop.noncopartisan.noncopartisan, 2), cex = .7)
text(2, 1, round(prop.nazi.noncopartisan, 2), cex = .7)
text(3, 1, round(prop.crime.noncopartisan, 2), cex = .7)
text(4, 1, round(prop.copartisan.noncopartisan, 2), cex = .7)
text(5, 1, round(prop.noncitizen.noncopartisan, 2), cex = .7)
text(6, 1, round(prop.immigrant.noncopartisan, 2), cex = .7)
text(7, 1, round(prop.dog.noncopartisan, 2), cex = .7)



###################################################
#Figure 3
###############################################

###############################################
#5 co-partisans Republicans
summary(model1 <- lm(flip ~ -1 + outpartisan1 + onenazi + onecrim + onedog + onenon + oneimm + copartisan1, data = data[data$copartisan5 == 1 & data$republican == 1,]))

se1 <- diag(sqrt(vcov(model1)))
points1 <- (model1$coefficients)

lower1 <- points1 - 1.96*se1
upper1 <- points1 + 1.96*se1

x.model1 <- seq(1, 7, 1)

par(mar = c(8, 4, 4, 2))
plot(x=x.model1, y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Co-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "golden\nretreiver", "non-citizen", "illegal\nimmigrant","copartisan"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)

segments(x0= x.model1, x1= x.model1, y0=rep(0, 7), y1=points1, lwd = 25, lend = "butt")
segments(x0= x.model1, x1= x.model1, y0=lower1, y1=upper1, lwd = 3, lend = "butt", col = "grey")
box()

abline(h = points1[1], lty = 2, col = "grey")
#summary(model1a <- lm(flip ~ onenazi + onecrim + onedog + onenon + oneimm + copartisan1, data = data[data$copartisan5 == 1 & data$republican == 1,]))
mtext(side=1, at = 4, line = 5, "Sacrificial Individual")
mtext(side=3, at = 4, line = .5, "Republican Respondents", col = "black", cex = .8)
text(1:7, 1, round(points1, 2), cex = .7)


###############################################
#5 co-partisans, Democrats
summary(model1 <- lm(flip ~ -1 + outpartisan1 + onenazi + onecrim + onedog + onenon + oneimm + copartisan1, data = data[data$copartisan5 == 1 & data$democrat == 1,]))

se1 <- diag(sqrt(vcov(model1)))
points1 <- (model1$coefficients)

lower1 <- points1 - 1.96*se1
upper1 <- points1 + 1.96*se1

x.model1 <- seq(1, 7, 1)

par(mar = c(8, 4, 4, 2))
plot(x=x.model1, y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Co-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "golden\nretreiver", "non-citizen", "illegal\nimmigrant","copartisan"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)

segments(x0= x.model1, x1= x.model1, y0=rep(0, 7), y1=points1, lwd = 25, lend = "butt")
segments(x0= x.model1, x1= x.model1, y0=lower1, y1=upper1, lwd = 3, lend = "butt", col = "grey")
box()

abline(h = points1[1], lty = 2, col = "grey")
points(x = c(4, 5, 6), y = upper1[c(4, 5, 6)]+.03, pch = 8, cex = .5)
mtext(side=1, at = 4, line = 5, "Sacrificial Individual")
mtext(side=3, at = 4, line = .5, "Democratic Respondents", col = "black", cex = .8)
text(1:7, 1, round(points1, 2), cex = .7)





###############################################
#5 out-partisans Republicans
summary(model1 <- lm(flip ~ -1 + outpartisan1 + onenazi + onecrim + onedog + onenon + oneimm + copartisan1, data = data[data$outpartisan5 == 1 & data$republican == 1,]))

se1 <- diag(sqrt(vcov(model1)))
points1 <- (model1$coefficients)

lower1 <- points1 - 1.96*se1
upper1 <- points1 + 1.96*se1

x.model1 <- seq(1, 7, 1)

par(mar = c(8, 4, 4, 2))
plot(x=x.model1, y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Out-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "golden\nretreiver", "non-citizen", "illegal\nimmigrant","copartisan"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)

segments(x0= x.model1, x1= x.model1, y0=rep(0, 7), y1=points1, lwd = 25, lend = "butt")
segments(x0= x.model1, x1= x.model1, y0=lower1, y1=upper1, lwd = 3, lend = "butt", col = "grey")
box()

abline(h = points1[1], lty = 2, col = "grey")
points(x = 7, y = upper1[7]+.03, pch = 8, cex = .5)
mtext(side=1, at = 4, line = 5, "Sacrificial Individual")
mtext(side=3, at = 4, line = .5, "Republican Respondents", col = "black", cex = .8)
text(1:7, 1, round(points1, 2), cex = .7)


#5 out-partiasns, Democrats
summary(model1 <- lm(flip ~ -1 + outpartisan1 + onenazi + onecrim + onedog + onenon + oneimm + copartisan1, data = data[data$outpartisan5 == 1 & data$democrat == 1,]))

se1 <- diag(sqrt(vcov(model1)))
points1 <- (model1$coefficients)

lower1 <- points1 - 1.96*se1
upper1 <- points1 + 1.96*se1

x.model1 <- seq(1, 7, 1)

par(mar = c(8, 4, 4, 2))
plot(x=x.model1, y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Out-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "golden\nretreiver", "non-citizen", "illegal\nimmigrant","copartisan"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)

segments(x0= x.model1, x1= x.model1, y0=rep(0, 7), y1=points1, lwd = 25, lend = "butt")
segments(x0= x.model1, x1= x.model1, y0=lower1, y1=upper1, lwd = 3, lend = "butt", col = "grey")
box()

abline(h = points1[1], lty = 2, col = "grey")
points(x = c(2, 5, 7), y = upper1[c(2, 5, 7)]+.03, pch = 8, cex = .5)
mtext(side=1, at = 4, line = 5, "Sacrificial Individual")
mtext(side=3, at = 4, line = .5, "Democratic Respondents", col = "black", cex = .8)
text(1:7, 1, round(points1, 2), cex = .7)



######################################################################################
####SUPPLEMENTAL MATERIALS#####
######################################################################################

#BALANCE TESTS
summary(model1 <- lm(fiverep ~ republican, data = data))
data$male <- ifelse(data$gender == "Male", 1, 0)
data$white <- ifelse(data$race == "White", 1, 0)
data$black <- ifelse(data$race == "Black", 1, 0)
data$hisp <- ifelse(data$race == "Hispanic", 1, 0)
data$age <-2018 - data$birthyr
data$faminc <- as.numeric(data$faminc_new)

t.test(republican ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(democrat ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(male ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(as.numeric(ideo5) ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(faminc ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(as.numeric(educ) ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(as.numeric(newsint) ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(white ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(as.numeric(pew_religimp) ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
t.test(age ~ fiverep, data = data[data$republican == 1 | data$democrat == 1,])
nrow(data[data$fiverep == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$fiverep == 0 & (data$republican == 1 | data$democrat == 1),])


summary(lm(republican ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(democrat ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(male ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(as.numeric(ideo5) ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(as.numeric(faminc) ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$faminc != 17 & (data$republican == 1 | data$democrat == 1),]))
summary(lm(as.numeric(educ) ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(as.numeric(newsint) ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[as.numeric(data$newsint) != 5 & (data$republican == 1 | data$democrat == 1),]))
summary(lm(white ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(as.numeric(pew_religimp) ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))
summary(lm(age ~ -1 + onerep + onedem + onecrim + oneimm + onenazi + onedog + onenon, data = data[data$republican == 1 | data$democrat == 1,]))

nrow(data[data$onerep == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$onedem == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$onecrim == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$oneimm == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$onenazi == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$onedog == 1 & (data$republican == 1 | data$democrat == 1),])
nrow(data[data$onenon == 1 & (data$republican == 1 | data$democrat == 1),])

data <- data[order(data$caseid),]
data$index <- unlist(tapply(data$caseid, data$caseid, function(x) 1:length(x)))

summary(model1 <- lm(totalqs ~ republican + male + as.numeric(ideo5) + as.numeric(newsint) + as.numeric(educ) + faminc + white + black + hisp + as.numeric(pew_religimp) + age, data = data[data$index ==  1 & data$faminc != 17 & as.numeric(data$ideo5) != 6 & as.numeric(data$newsint) != 5 & (data$republican == 1 | data$democrat == 1),]))
nobs(model1)


###########################################
# Only first case for issues of demand effects
###########################################

order <- read.csv("trolley_order.csv")
m <- match(data$caseid, order$caseid)
table(is.na(m))

#just to get caseid as last column to see on "head" command
data$caseid2 <- data$caseid

data$order.Q1 <- order$Q1[m]
data$order.Q2 <- order$Q2[m]
data$order.Q3 <- order$Q3[m]
data$order.Q4 <- order$Q4[m]

data$keep <- 0
data$keep <- ifelse(data$order.Q1 == 1 & data$fiverep == 1 & data$onerep == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 2 & data$fiverep == 1 & data$onedem == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 3 & data$fiverep == 1 & data$onecrim == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 4 & data$fiverep == 1 & data$oneimm == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 5 & data$fiverep == 1 & data$onenazi == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 6 & data$fiverep == 1 & data$onedog == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 7 & data$fiverep == 1 & data$onenon == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 8 & data$fivedem == 1 & data$onerep == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 9 & data$fivedem == 1 & data$onedem == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 10 & data$fivedem == 1 & data$onecrim == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 11 & data$fivedem == 1 & data$oneimm == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 12 & data$fivedem == 1 & data$onenazi == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 13 & data$fivedem == 1 & data$onedog == 1, 1, data$keep)
data$keep <- ifelse(data$order.Q1 == 14 & data$fivedem == 1 & data$onenon == 1, 1, data$keep)
data2 <- subset(data, keep == 1)


#first is 1 to be sacrificed, second is identify of 5
prop.copartisan.copartisan1 <- mean(data2$flip[data2$copartisan1 == 1 & data2$copartisan5 == 1], na.rm = T)
prop.noncopartisan.copartisan1 <- mean(data2$flip[data2$outpartisan1 == 1 & data2$copartisan5 == 1], na.rm = T)

prop.copartisan.noncopartisan1 <- mean(data2$flip[data2$copartisan1 == 1 & data2$outpartisan5 == 1], na.rm = T)
prop.noncopartisan.noncopartisan1 <- mean(data2$flip[data2$outpartisan1 == 1 & data2$outpartisan5 == 1], na.rm = T)

summary(model1 <- lm(flip ~ -1 + copartisan1, data=data2[data2$copartisan5 == 1,]))
l1 <- model1$coefficients - 1.96*sqrt(vcov(model1)[1])
u1 <- model1$coefficients + 1.96*sqrt(vcov(model1)[1])
summary(model2 <- lm(flip ~ -1 + outpartisan1, data=data2[data2$copartisan5 == 1,]))
l2 <- model2$coefficients - 1.96*sqrt(vcov(model2)[1])
u2 <- model2$coefficients + 1.96*sqrt(vcov(model2)[1])
summary(model3 <- lm(flip ~ -1 + copartisan1, data=data2[data2$outpartisan5 == 1,]))
l3 <- model3$coefficients - 1.96*sqrt(vcov(model3)[1])
u3 <- model3$coefficients + 1.96*sqrt(vcov(model3)[1])
summary(model4 <- lm(flip ~ -1 + outpartisan1, data=data2[data2$outpartisan5 == 1,]))
l4 <- model4$coefficients - 1.96*sqrt(vcov(model4)[1])
u4 <- model4$coefficients + 1.96*sqrt(vcov(model4)[1])


par(mar = c(5.1, 4.1, 4.1, 2.1))
plot(x=c(1,2,3,4), y = c(0, 1, 0, 1), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "Sacrificial Individual", xlim = c(.75, 4.25), ylim = c(0., 1.1))
axis(1, at = c(1, 2, 3, 4), labels = c("1-copartisan", "1-outpartisan", "1-copartisan", "1-outpartisan"))
axis(2, at = seq(0, 1, .2), las = 2)
text(1.5, 1.1, "To save 5-copartisans")
text(3.5, 1.1, "To save 5-outpartisans")
segments(x0=1, x1=1, y0=0, y1=prop.copartisan.copartisan1, lwd = 25, lend = "butt")
segments(x0=2, x1=2, y0=0, y1=prop.noncopartisan.copartisan1, lwd = 25, lend = "butt")
segments(x0=3, x1=3, y0=0, y1=prop.copartisan.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=4, x1=4, y0=0, y1=prop.noncopartisan.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=1, x1=1, y0=l1, y1=1, lwd = 3, lend = "butt", col = "grey")
segments(x0=2, x1=2, y0=l2, y1=1, lwd = 3, lend = "butt", col = "grey")
segments(x0=3, x1=3, y0=l3, y1=u3, lwd = 3, lend = "butt", col = "grey")
segments(x0=4, x1=4, y0=l4, y1=u4, lwd = 3, lend = "butt", col = "grey")
abline(v = 2.5, lty = 2, col = "light grey")
text(4, prop.noncopartisan.noncopartisan1 + .08, round(prop.noncopartisan.noncopartisan1, 2))
text(3, prop.copartisan.noncopartisan1 + .08, round(prop.copartisan.noncopartisan1, 2))
text(2, prop.noncopartisan.copartisan1 + .08, round(prop.noncopartisan.copartisan1, 2))
text(1, prop.copartisan.copartisan1 + .08, round(prop.copartisan.copartisan1, 2))
box()



############################################################
#Figure 1 split by party of respondent
#first is 1 to be sacrificed, second is identify of 5
############################################################
prop.copartisan.copartisan.r1 <- mean(data2$flip[data2$copartisan1 == 1 & data2$copartisan5 == 1 & data2$republican == 1], na.rm = T)
prop.noncopartisan.copartisan.r1 <- mean(data2$flip[data2$outpartisan1 == 1 & data2$copartisan5 == 1 & data2$republican == 1], na.rm = T)
prop.copartisan.noncopartisan.r1 <- mean(data2$flip[data2$copartisan1 == 1 & data2$outpartisan5 == 1 & data2$republican == 1], na.rm = T)
prop.noncopartisan.noncopartisan.r1 <- mean(data2$flip[data2$outpartisan1 == 1 & data2$outpartisan5 == 1 & data2$republican == 1], na.rm = T)

prop.copartisan.copartisan.d1 <- mean(data$flip[data2$copartisan1 == 1 & data2$copartisan5 == 1 & data2$democrat == 1], na.rm = T)
prop.noncopartisan.copartisan.d1 <- mean(data$flip[data2$outpartisan1 == 1 & data2$copartisan5 == 1 & data2$democrat == 1], na.rm = T)
prop.copartisan.noncopartisan.d1 <- mean(data$flip[data2$copartisan1 == 1 & data2$outpartisan5 == 1 & data2$democrat == 1], na.rm = T)
prop.noncopartisan.noncopartisan.d1 <- mean(data$flip[data2$outpartisan1 == 1 & data2$outpartisan5 == 1 & data2$democrat == 1], na.rm = T)

summary(model1 <- lm(flip ~ 1, data = data2[data2$copartisan1 == 1 & data2$copartisan5 == 1 & data2$republican == 1,]))
summary(model2 <- lm(flip ~ 1, data = data2[data2$outpartisan1 == 1 & data2$copartisan5 == 1 & data2$republican == 1,]))
summary(model3 <- lm(flip ~ 1, data = data2[data2$copartisan1 == 1 & data2$outpartisan5 == 1 & data2$republican == 1,]))
summary(model4 <- lm(flip ~ 1, data = data2[data2$outpartisan1 == 1 & data2$outpartisan5 == 1 & data2$republican == 1,]))
summary(model5 <- lm(flip ~ 1, data = data2[data2$copartisan1 == 1 & data2$copartisan5 == 1 & data2$democrat == 1,]))
summary(model6 <- lm(flip ~ 1, data = data2[data2$outpartisan1 == 1 & data2$copartisan5 == 1 & data2$democrat == 1,]))
summary(model7 <- lm(flip ~ 1, data = data2[data2$copartisan1 == 1 & data2$outpartisan5 == 1 & data2$democrat == 1,]))
summary(model8 <- lm(flip ~ 1, data = data2[data2$outpartisan1 == 1 & data2$outpartisan5 == 1 & data2$democrat == 1,]))
se1 <- sqrt(vcov(model1))
se2 <- sqrt(vcov(model2))
se3 <- sqrt(vcov(model3))
se4 <- sqrt(vcov(model4))
se5 <- sqrt(vcov(model5))
se6 <- sqrt(vcov(model6))
se7 <- sqrt(vcov(model7))
se8 <- sqrt(vcov(model8))

conf1 <- c(prop.copartisan.copartisan.r1 - 1.96*se1, prop.copartisan.copartisan.r1 + 1.96*se1)
conf2 <- c(prop.copartisan.copartisan.d1 - 1.96*se5, prop.copartisan.copartisan.d1 + 1.96*se5)
conf3 <- c(prop.noncopartisan.copartisan.r1 - 1.96*se2, prop.noncopartisan.copartisan.r1 + 1.96*se2)
conf4 <- c(prop.noncopartisan.copartisan.d1 - 1.96*se6, prop.noncopartisan.copartisan.d1 + 1.96*se6)
conf5 <- c(prop.copartisan.noncopartisan.r1 - 1.96*se3, prop.copartisan.noncopartisan.r1 + 1.96*se3)
conf6 <- c(prop.copartisan.noncopartisan.d1 - 1.96*se7, prop.copartisan.noncopartisan.d1 + 1.96*se7)
conf7 <- c(prop.noncopartisan.noncopartisan.r1 - 1.96*se4, prop.noncopartisan.noncopartisan.r1 + 1.96*se4)
conf8 <- c(prop.noncopartisan.noncopartisan.d1 - 1.96*se8, prop.noncopartisan.noncopartisan.d1 + 1.96*se8)

#begin the plot
plot(x=c(1,2,3,4), y = c(0, 1, 0, 1), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 To Save 5", xlab = "Sacrificial Individual", xlim = c(.75, 4.25), ylim = c(0, 1.1))
axis(1, at = c(1, 2, 3, 4), labels = c("1-copartisan", "1-outpartisan", "1-copartisan", "1-outpartisan"))
axis(2, at = seq(0, 1, .2), las = 2)
text(1.5, 1.1, "To save 5-copartisans")
text(3.5, 1.1, "To save 5-outpartisans")
segments(x0=.9, x1=.9, y0=0, y1=prop.copartisan.copartisan.r1, lwd = 25, lend = "butt")
segments(x0=1.1, x1=1.1, y0=0, y1=prop.copartisan.copartisan.d1, lwd = 25, lend = "butt", col = "grey")
segments(x0=1.9, x1=1.9, y0=0, y1=prop.noncopartisan.copartisan.r1, lwd = 25, lend = "butt")
segments(x0=2.1, x1=2.1, y0=0, y1=prop.noncopartisan.copartisan.d1, lwd = 25, lend = "butt", col = "grey")
segments(x0=2.9, x1=2.9, y0=0, y1=prop.copartisan.noncopartisan.r1, lwd = 25, lend = "butt")
segments(x0=3.1, x1=3.1, y0=0, y1=prop.copartisan.noncopartisan.d1, lwd = 25, lend = "butt", col = "grey")
segments(x0=3.9, x1=3.9, y0=0, y1=prop.noncopartisan.noncopartisan.r1, lwd = 25, lend = "butt")
segments(x0=4.1, x1=4.1, y0=0, y1=prop.noncopartisan.noncopartisan.d1, lwd = 25, lend = "butt", col = "grey")

segments(x0=.9, x1=.9, y0=conf1[1], y1=conf1[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=1.1, x1=1.1, y0=conf2[1], y1=conf2[2], lwd = 3, lend = "butt", col = "black")
segments(x0=1.9, x1=1.9, y0=conf3[1], y1=conf3[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=2.1, x1=2.1, y0=conf4[1], y1=conf4[2], lwd = 3, lend = "butt", col = "black")
segments(x0=2.9, x1=2.9, y0=conf5[1], y1=conf5[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=3.1, x1=3.1, y0=conf6[1], y1=conf6[2], lwd = 3, lend = "butt", col = "black")
segments(x0=3.9, x1=3.9, y0=conf7[1], y1=conf7[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=4.1, x1=4.1, y0=conf8[1], y1=conf8[2], lwd = 3, lend = "butt", col = "black")

text(x=.9, y=prop.copartisan.copartisan.r1 + .1, round(prop.copartisan.copartisan.r1, 2), cex = .7)
text(x=1.1, y=prop.copartisan.copartisan.d1 + .1, round(prop.copartisan.copartisan.d1, 2), cex = .7)
text(x=1.9, y=prop.noncopartisan.copartisan.r1 + .1, round(prop.noncopartisan.copartisan.r1, 2), cex = .7)
text(x=2.1, y=prop.noncopartisan.copartisan.d1 + .1, round(prop.noncopartisan.copartisan.d1, 2), cex = .7)
text(x=2.9, y=prop.copartisan.noncopartisan.r1 + .13, round(prop.copartisan.noncopartisan.r1, 2), cex = .7)
text(x=3.1, y=prop.copartisan.noncopartisan.d1 + .11, round(prop.copartisan.noncopartisan.d1, 2), cex = .7)
text(x=3.9, y=prop.noncopartisan.noncopartisan.r1 + .11, round(prop.noncopartisan.noncopartisan.r1, 2), cex = .7)
text(x=4.1, y=prop.noncopartisan.noncopartisan.d1 + .11, round(prop.noncopartisan.noncopartisan.d1, 2), cex = .7)

abline(v = 2.5, lty = 2, col = "light grey")
text(.9, .28, srt = 90, "Republican Respondents", col = "white")
text(1.1, .28, srt = 90, "Democratic Respondents", col = "black")

box()


###################################################
#add all other identities
#to save 5 copartisans
###################################################
prop.crime.copartisan1 <- mean(data2$flip[data2$onecrim == 1 & data2$copartisan5 == 1], na.rm = T)
prop.immigrant.copartisan1 <- mean(data2$flip[data2$oneimm == 1 & data2$copartisan5 == 1], na.rm = T)
prop.nazi.copartisan1 <- mean(data2$flip[data2$onenazi == 1 & data2$copartisan5 == 1], na.rm = T)
prop.dog.copartisan1 <- mean(data2$flip[data2$onedog == 1 & data2$copartisan5 == 1], na.rm = T)
prop.noncitizen.copartisan1 <- mean(data2$flip[data2$onenon == 1 & data2$copartisan5 == 1], na.rm = T)

summary(model <- lm(flip ~ onenazi + onecrim + copartisan1 + onenon + oneimm + onedog, data = data2[data2$copartisan5 == 1,]))

summary(model1 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$outpartisan1 == 1,]))
summary(model2 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$onenazi == 1,]))
summary(model3 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$onecrim == 1,]))
summary(model4 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$copartisan1 == 1,]))
summary(model5 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$onenon == 1,]))
summary(model6 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$oneimm == 1,]))
summary(model7 <- lm(flip ~ 1, data = data2[data2$copartisan5 == 1 & data2$onedog == 1,]))

se1 <- sqrt(vcov(model1))
se2 <- sqrt(vcov(model2))
se3 <- sqrt(vcov(model3))
se4 <- sqrt(vcov(model4))
se5 <- sqrt(vcov(model5))
se6 <- sqrt(vcov(model6))
se7 <- sqrt(vcov(model7))

conf1 <- c(prop.noncopartisan.copartisan1 - 1.96*se1, prop.noncopartisan.copartisan1 + 1.96*se1)
conf2 <- c(prop.nazi.copartisan1 - 1.96*se2, prop.nazi.copartisan1 + 1.96*se2)
conf3 <- c(prop.crime.copartisan1 - 1.96*se3, prop.crime.copartisan1 + 1.96*se3)
conf4 <- c(prop.copartisan.copartisan1 - 1.96*se4, prop.copartisan.copartisan1 + 1.96*se4)
conf5 <- c(prop.noncitizen.copartisan1 - 1.96*se5, prop.noncitizen.copartisan1 + 1.96*se5)
conf6 <- c(prop.immigrant.copartisan1 - 1.96*se6, prop.immigrant.copartisan1 + 1.96*se6)
conf7 <- c(prop.dog.copartisan1 - 1.96*se7, prop.dog.copartisan1 + 1.96*se7)


par(mar = c(8, 4, 4, 2))
plot(x=c(1,2,3,4,5,6,7), y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Co-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "copartisan", "non-citizen",  "illegal\nimmigrant", "golden\nretreiver"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)
segments(x0=1, x1=1, y0=0, y1=prop.noncopartisan.copartisan1, lwd = 25, lend = "butt")
segments(x0=2, x1=2, y0=0, y1=prop.nazi.copartisan1, lwd = 25, lend = "butt")
segments(x0=3, x1=3, y0=0, y1=prop.crime.copartisan1, lwd = 25, lend = "butt")
segments(x0=4, x1=4, y0=0, y1=prop.copartisan.copartisan1, lwd = 25, lend = "butt")
segments(x0=5, x1=5, y0=0, y1=prop.noncitizen.copartisan1, lwd = 25, lend = "butt")
segments(x0=6, x1=6, y0=0, y1=prop.immigrant.copartisan1, lwd = 25, lend = "butt")
segments(x0=7, x1=7, y0=0, y1=prop.dog.copartisan1, lwd = 25, lend = "butt")

segments(x0=1, x1=1, y0=conf1[1], y1=conf1[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=2, x1=2, y0=conf2[1], y1=conf2[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=3, x1=3, y0=conf3[1], y1=conf3[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=4, x1=4, y0=conf4[1], y1=conf4[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=5, x1=5, y0=conf5[1], y1=conf5[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=6, x1=6, y0=conf6[1], y1=conf6[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=7, x1=7, y0=conf7[1], y1=conf7[2], lwd = 3, lend = "butt", col = "grey")

abline(h = prop.noncopartisan.copartisan1, lty = 2, col = "grey")
mtext(side = 1, at = 4, line = 5, "Sacrificial Individual")
box()

text(1, 1, round(prop.noncopartisan.copartisan1, 2), cex = .7)
text(2, 1, round(prop.nazi.copartisan1, 2), cex = .7)
text(3, 1, round(prop.crime.copartisan1, 2), cex = .7)
text(4, 1, round(prop.copartisan.copartisan1, 2), cex = .7)
text(5, 1, round(prop.noncitizen.copartisan1, 2), cex = .7)
text(6, 1, round(prop.immigrant.copartisan1, 2), cex = .7)
text(7, 1, round(prop.dog.copartisan1, 2), cex = .7)




###################################################
#now for saving 5 outpartisans
###################################################
prop.crime.noncopartisan1 <- mean(data2$flip[data2$onecrim == 1 & data2$outpartisan5 == 1], na.rm = T)
prop.immigrant.noncopartisan1 <- mean(data2$flip[data2$oneimm == 1 & data2$outpartisan5 == 1], na.rm = T)
prop.nazi.noncopartisan1 <- mean(data2$flip[data2$onenazi == 1 & data2$outpartisan5 == 1], na.rm = T)
prop.dog.noncopartisan1 <- mean(data2$flip[data2$onedog == 1 & data2$outpartisan5 == 1], na.rm = T)
prop.noncitizen.noncopartisan1 <- mean(data2$flip[data2$onenon == 1 & data2$outpartisan5 == 1], na.rm = T)

summary(model <- lm(flip ~ onenazi + onecrim + copartisan1 + onenon + oneimm + onedog, data = data2[data2$outpartisan5 == 1,]))

summary(model1 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$outpartisan1 == 1,]))
summary(model2 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$onenazi == 1,]))
summary(model3 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$onecrim == 1,]))
summary(model4 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$copartisan1 == 1,]))
summary(model5 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$onenon == 1,]))
summary(model6 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$oneimm == 1,]))
summary(model7 <- lm(flip ~ 1, data = data2[data2$outpartisan5 == 1 & data2$onedog == 1,]))

se1 <- sqrt(vcov(model1))
se2 <- sqrt(vcov(model2))
se3 <- sqrt(vcov(model3))
se4 <- sqrt(vcov(model4))
se5 <- sqrt(vcov(model5))
se6 <- sqrt(vcov(model6))
se7 <- sqrt(vcov(model7))

conf1o <- c(prop.noncopartisan.noncopartisan1 - 1.96*se1, prop.noncopartisan.noncopartisan1 + 1.96*se1)
conf2o <- c(prop.nazi.noncopartisan1 - 1.96*se2, prop.nazi.noncopartisan1 + 1.96*se2)
conf3o <- c(prop.crime.noncopartisan1 - 1.96*se3, prop.crime.noncopartisan1 + 1.96*se3)
conf4o <- c(prop.copartisan.noncopartisan1 - 1.96*se4, prop.copartisan.noncopartisan1 + 1.96*se4)
conf5o <- c(prop.noncitizen.noncopartisan1 - 1.96*se5, prop.noncitizen.noncopartisan1 + 1.96*se5)
conf6o <- c(prop.immigrant.noncopartisan1 - 1.96*se6, prop.immigrant.noncopartisan1 + 1.96*se6)
conf7o <- c(prop.dog.noncopartisan1 - 1.96*se7, prop.dog.noncopartisan1 + 1.96*se7)

par(mar = c(8, 4, 4, 2))
plot(x=c(1,2,3,4,5,6,7), y = c(0, 1, 0, 1, 0, 1, 0), cex = 0, axes = F, ylab = "Proportion Sacrificing 1 to Save 5", xlab = "", xlim = c(.75, 7.25), main = "To Save 5 Out-partisans")
axis(1, at = c(1, 2, 3, 4, 5, 6, 7), labels = c("outpartisan", "neo-nazi", "criminal", "copartisan", "non-citizen",  "illegal\nimmigrant", "golden\nretreiver"), cex.axis = .8, las = 2)
axis(2, at = seq(0, 1, .2), las = 2)
segments(x0=1, x1=1, y0=0, y1=prop.noncopartisan.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=2, x1=2, y0=0, y1=prop.nazi.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=3, x1=3, y0=0, y1=prop.crime.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=4, x1=4, y0=0, y1=prop.copartisan.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=5, x1=5, y0=0, y1=prop.noncitizen.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=6, x1=6, y0=0, y1=prop.immigrant.noncopartisan1, lwd = 25, lend = "butt")
segments(x0=7, x1=7, y0=0, y1=prop.dog.noncopartisan1, lwd = 25, lend = "butt")

segments(x0=1, x1=1, y0=conf1o[1], y1=conf1o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=2, x1=2, y0=conf2o[1], y1=conf2o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=3, x1=3, y0=conf3o[1], y1=conf3o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=4, x1=4, y0=conf4o[1], y1=conf4o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=5, x1=5, y0=conf5o[1], y1=conf5o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=6, x1=6, y0=conf6o[1], y1=conf6o[2], lwd = 3, lend = "butt", col = "grey")
segments(x0=7, x1=7, y0=conf7o[1], y1=conf7o[2], lwd = 3, lend = "butt", col = "grey")

abline(h = prop.noncopartisan.noncopartisan1, lty = 2, col = "grey")
mtext(side = 1, at = 4, line = 5, "Sacrificial Individual")
box()

text(1, 1, round(prop.noncopartisan.noncopartisan1, 2), cex = .7)
text(2, 1, round(prop.nazi.noncopartisan1, 2), cex = .7)
text(3, 1, round(prop.crime.noncopartisan1, 2), cex = .7)
text(4, 1, round(prop.copartisan.noncopartisan1, 2), cex = .7)
text(5, 1, round(prop.noncitizen.noncopartisan1, 2), cex = .7)
text(6, 1, round(prop.immigrant.noncopartisan1, 2), cex = .7)
text(7, 1, round(prop.dog.noncopartisan1, 2), cex = .7)












